package com.shux.common.Interceptor;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.UnauthorizedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.shux.common.exception.ShuxRunTimeException;
import com.shux.utils.constants.Constant;

/**
 **********************************************
 *  描述：异常统一处理 返回页面
 * Simba.Hua 
 * 2017年6月17日
 **********************************************
**/
public class ShuxExceptionPageHandler implements HandlerExceptionResolver{
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
            Exception ex) {
        ShuxRunTimeException se = null;
        logger.error(ex.getMessage(),ex);
        if(ex instanceof ShuxRunTimeException){
            se = (ShuxRunTimeException)ex;
        } else if(ex instanceof IncorrectCredentialsException || ex instanceof UnknownAccountException){
            se = new ShuxRunTimeException(Constant.EXCEPTION_MESSAGE_USERNAME_PASSWORD_ISNOT_CORRECT);
        } else if(ex instanceof UnauthorizedException){
            se = new ShuxRunTimeException(Constant.EXCEPTION_MESSAGE_YOU_ARE_FORBIDDEN);
        }else{
            se = new ShuxRunTimeException(Constant.EXCEPTION_OTHER_ERROR);
        }
        
        ModelAndView mv = new ModelAndView();
        mv.addObject("exception", ex.getMessage());
        mv.setViewName("/commonjs/jsp/error");
        return mv;
    }
    

}
